﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Fis.Win.Forms
{
    public partial class BookInfoForm : BaseForm
    {
        public BookInfoForm()
        {
            InitializeComponent();
        }

        bool isBookReset = false;
        bool isAccReset = false;

        public override void InitializeBinding()
        {
            var data = ViewModel["data"] as DataTable;

            if (ViewState == Agile.Engine.View.ViewState.AddNew)
            {
                data.Rows.Add(data.NewRow());
                data.Rows[0]["FACCSTRUCT"] = "4-2-2";
                data.Rows[0]["FACCSYSTEM"] = "0";
                data.Rows[0]["FBOOKSTARTIME"] = DateTime.Parse(string.Format("{0}-{1}-1", DateTime.Now.Year, DateTime.Now.Month));
                data.Rows[0]["FBILLTYPE"] = "0";
                data.Rows[0]["FBOOKINITIALIZED"] = 0;
                data.Rows[0]["FCURRENCY"] = "defaultCurrencyRate";
            }

            Fis.Win.Utils.UIUtils.ClearBindings(dataLayoutControl1);
            Fis.Win.Utils.UIUtils.TagBindings(dataLayoutControl1, data);

            Fis.Win.Utils.UIUtils.LookUpBinding(FACCSYSTEMLookUpEdit, Fis.Win.Utils.CacheContext.GetCache("ACCKIND"), "caption", "key");
            Fis.Win.Utils.UIUtils.LookUpBinding(FBILLTYPETextEdit, Fis.Win.Utils.CacheContext.GetCache("BILLTYPEKIND"), "caption", "key");
            Fis.Win.Utils.UIUtils.LookUpBinding(editBWB, DefaultCurrency, "FNAME", "SID");
        }

        DataTable DefaultCurrency
        {
            get
            {
                DataTable data = new DataTable();
                data.Columns.Add("SID");
                data.Columns.Add("FCODE");
                data.Columns.Add("FNAME");

                data.Rows.Add(new object[] { "defaultCurrencyRate", "RMB", "人民币" });
                return data;
            }
        }

        public override void InitializeViewUI()
        {
            layoutControlGroup4.Enabled = !Fis.Win.Utils.AppSession.IsComplateInitialize;
            //结束初始化
            //if (Fis.Win.Utils.AppSession.IsComplateInitialize)
            //{
            //    FACCSTRUCTTextEdit.Properties.ReadOnly = true;
            //    FACCSYSTEMLookUpEdit.Properties.ReadOnly = true;
            //    FBILLTYPETextEdit.Properties.ReadOnly = true;
            //    FBOOKSTARTIMEDateEdit.Properties.ReadOnly = true;
            //    editBWB.Properties.ReadOnly = true;
            //}

        }

        private void BookInfoForm_Load(object sender, EventArgs e)
        {
            oldAccount = FACCSTRUCTTextEdit.Text.Trim();
            lockEdit = false;
        }

        private void bnOK_Click(object sender, EventArgs e)
        {
            Validate();

            if (!dxValidationProvider1.Validate())
            {
                return;
            }

            if (ViewState == Agile.Engine.View.ViewState.Modify)
            {
                //没有结束初始化
                if (isBookReset)
                {
                    if (DevExpress.XtraEditors.XtraMessageBox.Show(
                        "修改帐套信息将会清除所有已输入的数据，是否继续?", "提示", MessageBoxButtons.YesNo)
                        != System.Windows.Forms.DialogResult.Yes)
                    {
                        return;
                    }
                    ViewModel["action"] = "create";
                }
                else if (isAccReset)
                {
                    if (DevExpress.XtraEditors.XtraMessageBox.Show(
                        "确定修改科目级次吗?"
                        , "提示", MessageBoxButtons.YesNo)
                        != System.Windows.Forms.DialogResult.Yes)
                    {
                        return;
                    }
                    ViewModel["action"] = "account";
                    ViewModel["oldAccount"] = oldAccount;
                }
                else
                {
                    ViewModel["action"] = "update";
                }
            }

            if (ViewProvider.ExecuteUpdate(ViewModel))
            {
                ViewResult = DialogResult.OK;
                DevExpress.XtraEditors.XtraMessageBox.Show("保存成功");
                return;
            }

            DevExpress.XtraEditors.XtraMessageBox.Show(string.IsNullOrEmpty(Message) ? "发生未知错误，保存失败" : Message);

        }

        private void bnCancel_Click(object sender, EventArgs e)
        {
            ViewResult = DialogResult.Cancel;
            return;
        }

        bool lockEdit = true;
        private void BookReset_EditValueChanged(object sender, EventArgs e)
        {
            if (!lockEdit)
            {
                isBookReset = true;
            }

            var data = ViewModel["data"] as DataTable;
            data.Rows[0]["FACCSYSTEM"] = FACCSYSTEMLookUpEdit.EditValue;

        }

        string oldAccount = string.Empty;
        private void AccountReset_EditValueChanged(object sender, EventArgs e)
        {
            FACCSTRUCTTextEdit.EditValue = Agile.Engine.Utils.StringUtils.IsEmpty(FACCSTRUCTTextEdit.EditValue) ? "4-2-2" : FACCSTRUCTTextEdit.EditValue;
            isAccReset = !FACCSTRUCTTextEdit.Text.Trim().Equals(oldAccount);
            var data = ViewModel["data"] as DataTable;
            data.Rows[0]["FACCSTRUCT"] = FACCSTRUCTTextEdit.EditValue;

        }

        private void FBOOKSTARTIMEDateEdit_EditValueChanged(object sender, EventArgs e)
        {
            var data = ViewModel["data"] as DataTable;

            data.Rows[0]["FBOOKSTARTIME"] = FBOOKSTARTIMEDateEdit.EditValue ?? DateTime.Parse(string.Format("{0}-{1}-1", DateTime.Now.Year, DateTime.Now.Month));

            if (FBOOKSTARTIMEDateEdit.EditValue != null && FBOOKSTARTIMEDateEdit.EditValue != DBNull.Value)
                data.Rows[0]["FBOOKRANGE"] = ((DateTime)FBOOKSTARTIMEDateEdit.EditValue).Month;

        }
    }
}
